package cn.stylefeng.guns.modular.storeProcurement.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.storeProcurement.entity.StoreProcurementDetaild;
import cn.stylefeng.guns.modular.storeProcurement.mapper.StoreProcurementDetaildMapper;
import cn.stylefeng.guns.modular.storeProcurement.model.params.StoreProcurementDetaildParam;
import cn.stylefeng.guns.modular.storeProcurement.model.result.StoreProcurementDetaildResult;
import cn.stylefeng.guns.modular.storeProcurement.service.StoreProcurementDetaildService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 门店采购单明细表 服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-09-02
 */
@Service
public class StoreProcurementDetaildServiceImpl extends ServiceImpl<StoreProcurementDetaildMapper, StoreProcurementDetaild> implements StoreProcurementDetaildService {

    @Override
    public void add(StoreProcurementDetaildParam param){
        StoreProcurementDetaild entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StoreProcurementDetaildParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StoreProcurementDetaildParam param){
        StoreProcurementDetaild oldEntity = getOldEntity(param);
        StoreProcurementDetaild newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StoreProcurementDetaildResult findBySpec(StoreProcurementDetaildParam param){
        return null;
    }

    @Override
    public List<StoreProcurementDetaildResult> findListBySpec(StoreProcurementDetaildParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StoreProcurementDetaildParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findGoodsPageBySpec(StoreProcurementDetaildParam storeProcurementDetaildParam, String condition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customGoodsPageList(pageContext, storeProcurementDetaildParam,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void deleteByGoodsId(StoreProcurementDetaildParam storeProcurementDetaildParam) {
        this.baseMapper.deleteByGoodsId(storeProcurementDetaildParam.getOrderOutNumber(), storeProcurementDetaildParam.getGoodsId());
    }

    @Override
    public List<StoreProcurementDetaildResult> findListByGoodsId(String orderOutNumber, Long goodsId) {
        return this.baseMapper.findListByGoodsId(orderOutNumber, goodsId);
    }



    private Serializable getKey(StoreProcurementDetaildParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StoreProcurementDetaild getOldEntity(StoreProcurementDetaildParam param) {
        return this.getById(getKey(param));
    }

    private StoreProcurementDetaild getEntity(StoreProcurementDetaildParam param) {
        StoreProcurementDetaild entity = new StoreProcurementDetaild();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
